<?php
/**
 * User: krisbuist
 * Date: 2/3/12
 * Time: 1:17 PM
 */
class SearchResult
{
    /**
     * @var int
     */
    protected $currentPage;

    /**
     * @var int
     */
    protected $resultsPerPage = 10;

    /**
     * @var int
     */
    protected $totalNumberOfPages;

    /**
     * @var int
     */
    protected $totalNumberOfResults;

    /**
     * @var \Doctrine\ORM\EntityManager
     */
    protected $em;

    /**
     * @var \Doctrine\ORM\Query
     */
    protected $resultQuery;

    /**
     * @var \Doctrine\ORM\Query
     */
    protected $countQuery;

    /**
     * @param \Doctrine\ORM\EntityManager $em
     */
    public function __construct($em = null)
    {
        $this->em = $em;
    }

    /**
     * @param $page
     * @return array
     */
    public function getResults($page)
    {
        if ($this->resultQuery != null)
        {
            $this->resultQuery->setFirstResult(($page-1) * $this->resultsPerPage);
            return $this->resultQuery->getResult();
        }
        else
        {
            return array();
        }
    }

    public function getTotalNumberOfResults()
    {
        if(!isset($this->totalNumberOfResults))
        {
            $this->totalNumberOfResults = $this->countQuery->getSingleScalarResult();
            $this->totalNumberOfPages = ceil($this->totalNumberOfResults / $this->resultsPerPage);
        }
        return $this->totalNumberOfResults;
    }

    /**
     * @return int
     */
    public function getTotalNumberOfPages()
    {
        return $this->totalNumberOfPages;
    }
}
